package org.ielts.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.ielts.domain.base.Classroom;
import org.ielts.domain.base.CourseType;
import org.ielts.domain.base.Lesson;
import org.ielts.domain.base.Teaching;
import org.ielts.domain.log.OrderLessonLog;
import org.ielts.domain.report.LessonQuery;
import org.ielts.domain.report.LessonWithListenerCount;
import org.ielts.domain.report.TeacherBlankLessonReport;
import org.ielts.domain.security.User;
import org.ielts.profile.IeltsProfile;
import org.ielts.repository.ClassroomRepository;
import org.ielts.repository.CourseTypeRepository;
import org.ielts.repository.LessonRepository;
import org.ielts.repository.OrderLessonLogRepository;
import org.ielts.repository.TeachingRepository;
import org.ielts.repository.security.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yifeng.ProfileContext;
import com.yifeng.page.table.Paginations;
import com.yifeng.profile.Profile;

@Service
@Transactional
public class LessonService {

	@Autowired
	ClassroomRepository classroomRepository;

	@Autowired
	CourseTypeRepository courseTypeRepository;

	@Autowired
	TeachingRepository teachingRepository;

	@Autowired
	UserRepository userRepository;

	@Autowired
	LessonRepository lessonRepository;

	public Lesson createLesson(int no, Date beginTime, Date endTime, String name, String title, String[] teachers,
			String classroomId, String courseTypeId) {
		Lesson lesson = new Lesson();
		lesson.setBeginTime(beginTime);
		lesson.setEndTime(endTime);
		lesson.setName(name);
		lesson.setTitle(title);
		lesson.setNo(no);

		for (String teacherId : teachers) {
			if (StringUtils.isBlank(teacherId)) {
				continue;
			}
			User teacher = userRepository.findOne(teacherId);
			Teaching teaching = new Teaching();
			teaching.setTeacher(teacher);
			teaching.setBeginTime(beginTime);
			teaching.setEndTime(endTime);

			teachingRepository.save(teaching);

			lesson.getTeachings().add(teaching);
		}

		CourseType courseType = courseTypeRepository.findOne(courseTypeId);
		Classroom classroom = classroomRepository.findOne(classroomId);

		lesson.setCourseType(courseType);
		lesson.setClassroom(classroom);

		lessonRepository.save(lesson);

		return lesson;
	}

	public Lesson createLesson(int no, Date beginTime, Date endTime, String name, String classroomId,
			String courseTypeId, Collection<Teaching> teachings) {
		Lesson lesson = new Lesson();
		lesson.setBeginTime(beginTime);
		lesson.setEndTime(endTime);
		lesson.setName(name);
		lesson.setNo(no);

		for (Teaching teaching : teachings) {
			teachingRepository.save(teaching);
			lesson.getTeachings().add(teaching);
		}

		CourseType courseType = new CourseType();
		courseType.setId(courseTypeId);

		Classroom classroom = new Classroom();
		classroom.setId(classroomId);

		lesson.setCourseType(courseType);
		lesson.setClassroom(classroom);

		lessonRepository.save(lesson);

		return lesson;
	}

	public Iterable<Lesson> findBetweenTime(Date beginTime, Date endTime) {
		ArrayList<String> props = new ArrayList<String>();
		props.add("beginTime");
		props.add("no");
		props.add("name");
		props.add("endTime");

		Sort sort = new Sort(Order.create(Direction.ASC, props));

		String campusId = ((IeltsProfile) Profile.getInstance().getProfile()).getCampus();
		if (StringUtils.isBlank(campusId)) {
			Iterable<Lesson> lessons = lessonRepository.findBetweenTime(beginTime, endTime, sort);
			return lessons;
		} else {
			Iterable<Lesson> lessons = lessonRepository.findBetweenTime(beginTime, endTime, campusId, sort);

			return lessons;
		}
	}

	public Lesson modifyLesson(int no, String id, Date beginTime, Date endTime, String name, String classroomId,
			String courseTypeId, Collection<Teaching> teachings) {
		Lesson lesson = lessonRepository.findOne(id);
		// 修改属性
		lesson.setBeginTime(beginTime);
		lesson.setEndTime(endTime);
		lesson.setName(name);
		lesson.setNo(no);

		CourseType courseType = courseTypeRepository.findOne(courseTypeId);

		Classroom classroom = classroomRepository.findOne(classroomId);

		lesson.setCourseType(courseType);
		lesson.setClassroom(classroom);

		// 去掉原有授课记录
		for (Teaching teaching : lesson.getTeachings()) {
			teachingRepository.delete(teaching);
		}

		for (Teaching teaching : teachings) {
			teaching.setLesson(lesson);
			teachingRepository.save(teaching);

			lesson.getTeachings().add(teaching);
		}

		return lesson;
	}

	public Lesson findOne(String id) {
		Lesson lesson = lessonRepository.findOne(id);
		return lesson;
	}

	public Lesson cancelLesson(String id) {
		Lesson lesson = lessonRepository.findOne(id);
		lesson.setCanceled(true);
		return lesson;
	}

	public void removeLesson(String id) {
		lessonRepository.delete(id);
	}

	public Lesson resumeLesson(String id) {
		Lesson lesson = lessonRepository.findOne(id);
		lesson.setCanceled(false);
		return lesson;
	}

	public Iterable<Lesson> findBetweenTimeAndClassroom(Date beginTime, Date endTime, String classroomId) {

		Iterable<Lesson> lessons = lessonRepository.findBetweenTimeAndClassroom(beginTime, endTime, classroomId);
		return lessons;

	}

	public Iterable<Lesson> findBetweenDateAndCourseType(Date startDate, Date endDate, String courseTypeId) {
		Iterable<Lesson> lessons = lessonRepository.findBetweenDateAndCourseType(startDate, endDate, courseTypeId);
		return lessons;
	}

	public Page<Lesson> findLesson(LessonQuery lessonQuery, Pageable paginations) {

		String campusId = ((IeltsProfile) Profile.getInstance().getProfile()).getCampus();
		if (StringUtils.isNotBlank(campusId)) {
			lessonQuery.setCampus(campusId);
		}
		Page<Lesson> lessons = lessonRepository.findLesson(lessonQuery, paginations);

		return lessons;
	}

	public long countLesson(LessonQuery lessonQuery) {
		long count = lessonRepository.countLesson(lessonQuery);
		return count;
	}

	public List<TeacherBlankLessonReport> blankLessonReport() {
		List<TeacherBlankLessonReport> report = new ArrayList<TeacherBlankLessonReport>();
		List<TeacherBlankLessonReport> temp = lessonRepository.findTeacherLessonCount("%教师");
		for (TeacherBlankLessonReport re : temp) {
			System.out.println(re.getTecherId());
			re.setBlankLessonNum(lessonRepository.getBlankLessionNumByTeacher(re.getTecherId()));
			report.add(re);
		}
		return report;
	}

	public Iterable<LessonWithListenerCount> findReportBetweenTimeAndClassroom(Date beginTime, Date endTime,
			String classroomId) {
		Iterable<LessonWithListenerCount> reports = lessonRepository.findReportBetweenTimeAndClassroom(beginTime,
				endTime, classroomId);

		return reports;
	}

	public Iterable<LessonWithListenerCount> findReportBetweenDateAndCourseType(Date beginTime, Date endTime,
			String courseTypeId) {
		String campusId = ((IeltsProfile) Profile.getInstance().getProfile()).getCampus();
		if (StringUtils.isBlank(campusId)) {
			Iterable<LessonWithListenerCount> reports = lessonRepository.findReportBetweenDateAndCourseType(beginTime,
					endTime, courseTypeId);

			return reports;
		} else {
			Iterable<LessonWithListenerCount> reports = lessonRepository.findReportBetweenDateAndCourseTypeAndCampus(
					beginTime, endTime, courseTypeId, campusId);

			return reports;
		}
	}

	@Autowired
	OrderLessonLogRepository orderLessonLogRepository;

	public Iterable<LessonWithListenerCount> findReportBetweenDate(Date beginTime, Date endTime) {
		String campusId = ((IeltsProfile) Profile.getInstance().getProfile()).getCampus();
		Iterable<Lesson> lessons;
		if (StringUtils.isBlank(campusId)) {
			lessons = lessonRepository.findBetweenTime(beginTime, endTime);
		} else {
			lessons = lessonRepository.findBetweenTime(beginTime, endTime, campusId);
		}

		ArrayList<LessonWithListenerCount> list = new ArrayList<LessonWithListenerCount>();

		for (Lesson lesson : lessons) {
			/*
			 * Iterable<OrderLessonLog> orderLessonLogs =
			 * orderLessonLogRepository.findOrderLessonLogByLesson(lesson
			 * .getId());
			 */
			LessonWithListenerCount e = new LessonWithListenerCount();
			// e.setCount(((List) orderLessonLogs).size());
			e.setLesson(lesson);
			// e.setOrderLessonLogs(orderLessonLogs);

			list.add(e);
		}

		return list;
	}

	public Iterable<Lesson> findReportByIds(String[] lessonId) {
		if (lessonId == null || lessonId.length == 0) {
			return new ArrayList<Lesson>();
		}

		Iterable<Lesson> lessons = lessonRepository.findAll(Arrays.asList(lessonId));

		return lessons;
	}
}
